﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kingdee.BOS.Contracts;
using Kingdee.BOS;
using Kingdee.BOS.App.Data;



namespace CHR.App.Core.Common
{
    public class TempTableService : CHR.Contract.Common.ITempTableService
    {
        public string CreateTable(Context ctx, IEnumerable<object> myList)
        {
            IDBService service = Kingdee.BOS.App.ServiceHelper.GetService<IDBService>();

            string tableName = service.CreateTemporaryTableName(ctx);
            string createTable = string.Format("CREATE TABLE {0} (FID INT)", tableName);
            using (KDTransactionScope trans1 = new KDTransactionScope(System.Transactions.TransactionScopeOption.RequiresNew))
            {
                DBUtils.Execute(ctx, createTable);
                trans1.Complete();
            }

            List<string> sqlList = new List<string>();
            string insert = "insert into  " + tableName + "  values({1})";
            foreach (var o in myList)
            {
                sqlList.Add(string.Format(insert, o));
            }
            DBUtils.ExecuteBatch(ctx, sqlList, 500);
            return tableName;
        }
    }
}
